﻿namespace JavaScript8000
{
    using System;
    using System.Windows.Forms;

    using JavaScript8000.Parser;
    using JavaScript8000.Scaner;

    /// <summary>
    /// The parser form.
    /// </summary>
    public partial class MagicForm : Form
    {
        #region Constants and Fields

        /// <summary>
        /// True magic box.
        /// </summary>
        private readonly MagicBox.MagicBox magicBox;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MagicForm"/> class.
        /// </summary>
        /// <param name="scResults">
        /// The scan results.
        /// </param>
        /// <param name="tree">
        /// The grammar tree.
        /// </param>
        public MagicForm(ScanResults scResults, GrammarNode tree)
        {
            this.InitializeComponent();

            if (scResults == null)
            {
                throw new ArgumentNullException("ScanResults shouldn't be null!");
            }

            if (tree == null)
            {
                throw new ArgumentNullException("tree shouldn't be null!");
            }

            // Initialize magic.
            this.magicBox = new MagicBox.MagicBox(scResults, tree);

            // Show tree.
            this.treeView1.Nodes.Add(this.magicBox.GetTree());
            this.treeView1.ExpandAll();

            this.variablesTextBox.Text = this.magicBox.GetVariblesList();

            this.Closing += delegate { this.magicBox.Deinitialize(); };
        }

        #endregion

        #region Methods

        /// <summary>
        /// Collapse tree view.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.
        /// </param>
        private void CloseAllToolStripMenuItemClick(object sender, EventArgs e)
        {
            this.treeView1.CollapseAll();
        }

        /// <summary>
        /// Expand tree view.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.
        /// </param>
        private void OpenAllToolStripMenuItemClick(object sender, EventArgs e)
        {
            this.treeView1.ExpandAll();
        }

        /// <summary>
        /// Show grammar form.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.
        /// </param>
        private void ShowGrammarToolStripMenuItemClick(object sender, EventArgs e)
        {
            BrainFuck form = Grammar.GetForm();
            if (!form.Visible)
            {
                form.Show();
            }
        }

        /// <summary>
        /// Handles the Click event of the stepToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.
        /// </param>
        private void StepToolStripMenuItemClick(object sender, EventArgs e)
        {
            if (!this.magicBox.IsExecuted)
            {
                if (!this.magicBox.DoStep())
                {
                    this.stepToolStripMenuItem.Enabled = false;
                    this.startToolStripMenuItem.Enabled = false;
                }

                this.variablesTextBox.Text = this.magicBox.GetVariblesList();
                this.logTextBox.Text = this.magicBox.Logs;
                this.treeView1.Nodes.Clear();
                this.treeView1.Nodes.Add(this.magicBox.GetTree());
                this.treeView1.ExpandAll();
            }
            else
            {
                this.stepToolStripMenuItem.Enabled = false;
                this.startToolStripMenuItem.Enabled = false;
                MessageBox.Show("Success!");
            }
        }

        /// <summary>
        /// Handles the Click event of the startToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            while (this.magicBox.DoStep()) { }

            this.variablesTextBox.Text = this.magicBox.GetVariblesList();
            this.logTextBox.Text = this.magicBox.Logs;
            this.treeView1.Nodes.Clear();
            this.treeView1.Nodes.Add(this.magicBox.GetTree());
            this.treeView1.ExpandAll();

            this.stepToolStripMenuItem.Enabled = false;
            this.startToolStripMenuItem.Enabled = false;

            if (this.magicBox.IsExecuted)
            {
                MessageBox.Show("Success!");
            }
            else
            {
                MessageBox.Show("Error!");
            }
        }

        #endregion
    }
}